//
//  TWShareViewController.swift
//  EyepetizerImitative
//
//  Created by 田伟 on 2017/4/24.
//  Copyright © 2017年 个人. All rights reserved.
//

import UIKit
import SnapKit

class TWShareViewController: UIViewController {

    fileprivate lazy var centerView : UIView = UIView()
    fileprivate lazy var pageControl : UIPageControl = UIPageControl()
    fileprivate lazy var iconScrollView : UIScrollView = UIScrollView()
    var model : TWVideoCollectionOfAuthorWithCoverModel?{
        didSet {
            guard let model = model else {
                return
            }
            print(model.header!.title!)
        }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        setupTitle()
        setupCenterView()
        setupCancelButton()
    }
}


extension TWShareViewController {
    fileprivate func setupTitle(){
        let title = UILabel()
        title.text = "分享"
        title.font = UIFont(name: "FZLTZCHJW--GB1-0", size: 16)
        view.addSubview(title)
        title.snp.makeConstraints { (make) in
            make.top.equalTo(self.view).offset(30)
            make.centerX.equalTo(self.view)
        }
    }
    fileprivate func setupCenterView(){
        view.addSubview(centerView)
        let centerViewW : CGFloat = TWScreenWidth - 2 * 30
        centerView.snp.makeConstraints { (make) in
            make.centerX.centerY.equalTo(self.view)
            make.height.equalTo(120)
            make.width.equalTo(centerViewW)
        }
        let topLine : UILabel = UILabel()
        topLine.backgroundColor = UIColor.darkGray.withAlphaComponent(0.4)
        centerView.addSubview(topLine)
        topLine.snp.makeConstraints { (make) in
            make.top.width.left.equalTo(centerView)
            make.height.equalTo(1)
        }
        let bottomLine : UILabel = UILabel()
        bottomLine.backgroundColor = UIColor.darkGray.withAlphaComponent(0.4)
        centerView.addSubview(bottomLine)
        bottomLine.snp.makeConstraints { (make) in
            make.bottom.width.left.equalTo(centerView)
            make.height.equalTo(1)
        }
        
        // 添加滚动视图
        let scrollViewW = centerViewW - 2 * 20
        iconScrollView.showsHorizontalScrollIndicator = false
        iconScrollView.isPagingEnabled = true
        iconScrollView.delegate = self
        centerView.addSubview(iconScrollView)
        iconScrollView.snp.makeConstraints { (make) in
            make.centerX.equalTo(centerView)
            make.top.equalTo(centerViewW).offset(10)
            make.width.equalTo(scrollViewW)
            make.height.equalTo(100)
        }
        let iconNameArray : [String] = ["微信","朋友圈","QQ","QQ空间","微博","复制链接"]
        let imageNameArray : [String] = ["action_share_weixin_grey_44x44_","action_share_moment_grey_44x44_","action_share_qq_grey_44x44_","action_share_qzone_grey_44x44_","action_share_weibo_grey_44x44_","action_share_link_grey_44x44_"]
        // 添加分享按钮
        var i  = 0
        let BtnW = scrollViewW / 5.0
        for name in iconNameArray {
            let button : TWIconButton = TWIconButton(type: .custom)
            button.setImage(UIImage(named: imageNameArray[i]), for: .normal)
            button.setTitle(name, for: .normal)
            button.titleLabel?.font = UIFont(name: "FZLTXIHJW--GB1-0", size: 10)
            button.setTitleColor(UIColor.darkGray, for: .normal)
            button.frame = CGRect(x: CGFloat(i) * BtnW, y: 0, width: BtnW, height: 44 + 20)
            button.addTarget(self, action: #selector(shareButtonClick(button:)), for: .touchUpInside)
            button.tag = 1111 + i
            iconScrollView.addSubview(button)
            i += 1
        }
        iconScrollView.contentSize = CGSize(width: 2 * scrollViewW, height: 0)
        
        // 添加指示器
        pageControl.numberOfPages = 2
        pageControl.currentPage = 0
        pageControl.pageIndicatorTintColor = UIColor.lightGray      // 非选中
        pageControl.currentPageIndicatorTintColor = UIColor.black   // 选中的
        centerView.addSubview(pageControl)
        pageControl.snp.makeConstraints { (make) in
            make.bottom.centerX.equalTo(centerView)
            make.height.equalTo(30)
            make.centerY.width.equalTo(iconScrollView)
        }
    }
    @objc fileprivate func shareButtonClick(button : TWIconButton){
        switch button.tag {
        case 1111:
            print("微信")
        case 1112:
            print("朋友圈")
        case 1113:
            print("QQ")
        case 1114:
            print("QQ空间")
        case 1115:
            print("微博")
        case 1116:
            print("复制链接")
        default:
            print("无")
        }
    }
    
    fileprivate func setupCancelButton(){
        let cancelButton = UIButton(type: .custom)
        cancelButton.setTitle("取消", for: .normal)
        cancelButton.titleLabel?.font = UIFont(name: "FZLTXIHJW--GB1-0", size: 16)
        cancelButton.addTarget(self, action: #selector(cancelButtonClick), for: .touchUpInside)
        cancelButton.setTitleColor(UIColor.black, for: .normal)
        view.addSubview(cancelButton)
        cancelButton.snp.makeConstraints { (make) in
            make.bottom.equalTo(self.view).offset(-30)
            make.centerX.equalTo(self.view)
        }
    }
    @objc fileprivate func cancelButtonClick(){
        dismiss(animated: true, completion: nil)
    }
}

extension TWShareViewController : UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let page : Int = Int(scrollView.contentOffset.x / scrollView.frame.size.width)
        pageControl.currentPage = page
    }
}
